package nc.tb.pub.tree;

import java.util.ArrayList;
import java.util.HashMap;

import nc.tb.dim.DimDef;
import nc.tb.dim.DimMember;
import nc.tb.dim.DimView;
import nc.tb.dim.ViewRelation;
import nc.tb.dim.data.DimManager;
import nc.tb.dim.data.ViewManager;
import nc.tb.perm.PrvlCtl;
import nc.tb.pub.IBusiObj;
import nc.tb.pub.util.DimTools;
import nc.tb.pub.util.NtbLogger;
import nc.tb.pub.util.SortTool;
import nc.ui.pub.ClientEnvironment;
import nc.vo.pub.BusinessException;

public class DimViewTreeModel extends TbFCTreeModel {
	
	private AbstractTreePanel m_parentPanel;
	private HashMap<DimDef,DimView> hm_dd2dv = new HashMap<DimDef,DimView>();
	
	public DimViewTreeModel(DimDef dd, DimView dv) {
		super(new TbTreeNode(dd), null);
		addDimView(dd, dv);
	}
	
	public DimViewTreeModel(DimDef dd, DimView dv, DimMember root) {
		super(new TbTreeNode(dd), null);
		addDimViewSubTree(dd, dv, root);
	}
	
	public DimViewTreeModel(TbTreeNode root) {
		super(root, null);
	}
	
	public DimViewTreeModel(TbTreeNode root, DimDef[] vos) {
		super(root, vos);
	}
	
	public void setParentPanel(AbstractTreePanel treePanel) {
		m_parentPanel = treePanel;
	}
	
	public String getTitle() {
		TbTreeNode root = (TbTreeNode)getRoot();
		Object obj = root.getUserObject();
		if (obj instanceof IBusiObj)
			return ((IBusiObj)obj).getBusiObj().getObjname();
		else if (obj instanceof String)
			return (String)obj;
		else
			return obj.toString();
	}
	
	public void addDimView(DimDef dd, DimView dv) {
		hm_dd2dv.put(dd, dv);
		try {
			addDimMemByDimDefPK(DimTools.getViewRelaOrDimMembers(dd, dv, ClientEnvironment.getInstance().getUser(), 
					PrvlCtl.READ_ONLY, true, true, null), dd.getPrimaryKey());
		}
		catch (BusinessException be) {
			NtbLogger.printException(be);
		}
//		if (dv != null) {
//			try {
//				ViewRelation[] vrs = ViewManager.getViewRelations(dv);
//				SortTool.sortByCode(vrs);
//				addDimMemByDimDefPK(vrs, dd.getPrimaryKey());
//			}
//			catch (BusinessException be) {
//				NtbLogger.printException(be);
//			}
//		}
//		else {
//			try {
//				DimMember[] dms = DimManager.getDimMembers(dd);
//				SortTool.sortByCode(dms);
//				addDimMemByDimDefPK(dms, dd.getPrimaryKey());
//			}
//			catch (BusinessException be) {
//				NtbLogger.printException(be);
//			}
//		}
	}
	
	public TbTreeNode addDimViewSubTree(DimDef dd, DimView dv, DimMember root) {
		if (root == null) {
			addDimView(dd, dv);
			return null;
		}
		hm_dd2dv.put(dd, dv);
		if (dv != null) {
			try {
				ViewRelation[] vrRoot = ViewManager.getViewRelationsByMember(dv, new DimMember[]{root});
				if (vrRoot == null || vrRoot.length != 1) {
					NtbLogger.print(root+" ViewRelation not find in DimView "+dv);
					return null;
				}
				ViewRelation[] vrs = ViewManager.getViewRelations(dv);
				try{
					SortTool.sortByCode(vrs);
				}catch(Exception ex){
					NtbLogger.printException(ex);
				}
				TbFCTreeModel temp = new TbFCTreeModel(dv, vrs);
				TbTreeNode node = temp.getNodeByVo(vrRoot[0]);
				if (node == null) {
					NtbLogger.print(root+" ViewRelation node not find in DimView "+dv);
					return null;
				}
				TbTreeNode fNode = getNodeByVo(dd);
				if (fNode == null)
					fNode = (TbTreeNode)getRoot();
				insertNodeInto(node, fNode, fNode.getChildCount());
				return node;
			}
			catch (BusinessException be) {
				NtbLogger.printException(be);
				return null;
			}
		}
		else {
			try {
				DimMember[] dms = DimManager.getDimMembers(dd);
				SortTool.sortByCode(dms);
				TbFCTreeModel temp = new TbFCTreeModel(dd, dms);
				addDimMemByDimDefPK(dms, dd.getPrimaryKey());
				TbTreeNode node = temp.getNodeByVo(root);
				if (node == null) {
					NtbLogger.print(root+" DimMember node not find in DimDef "+dd);
					return null;
				}
				TbTreeNode fNode = getNodeByVo(dd);
				if (fNode == null)
					fNode = (TbTreeNode)getRoot();
				insertNodeInto(node, fNode, fNode.getChildCount());
				return node;
			}
			catch (BusinessException be) {
				NtbLogger.printException(be);
				return null;
			}
		}
	}
	
	public DimView getDimView(DimDef dd) {
		return (DimView)hm_dd2dv.get(dd);
	}
	
	public DimMember getSelectBusiObj() {
		if (m_parentPanel != null) {
			IBusiObj obj = m_parentPanel.getSelectBusiObj();
			if (obj instanceof DimMember)
				return (DimMember)obj;
			else if (obj instanceof ViewRelation) {
				try {
					return ((ViewRelation)obj).getDimMember();
				}
				catch (BusinessException be) {
					NtbLogger.printException(be);
				}
			}
		}
		return null;
	}
	
	public DimMember[] getSelectBusiObjs() {
		if (m_parentPanel != null) {
			IBusiObj[] objs = m_parentPanel.getSelectBusiObjs();
			ArrayList<DimMember> al = new ArrayList<DimMember>();
			if (objs != null) {
				for (int i=0; i<objs.length; i++) {
					if (objs[i] instanceof DimMember)
						al.add((DimMember)objs[i]);
					else if (objs[i] instanceof ViewRelation) {
						try {
							al.add(((ViewRelation)objs[i]).getDimMember());
						}
						catch (BusinessException be) {
							NtbLogger.printException(be);
						}
					}
				}
				return al.toArray(new DimMember[0]);
			}
		}
		return null;
	}
	
	public void setEnableTreeNodes(IBusiObj[] enableVos, boolean isEnable) {
		ArrayList<DimMember> al = new ArrayList<DimMember>();
		ArrayList<IBusiObj> al2 = new ArrayList<IBusiObj>();
		for (int i=0; i<enableVos.length; i++) {
			if (enableVos[i] instanceof DimMember)
				al.add((DimMember)enableVos[i]);
			al2.add(enableVos[i]);
		}
		if (!al.isEmpty()) {
			try {
				ViewRelation[] vrs = ViewManager.getViewRelationsByMember(getDimView(al.get(0).getDimendef()), (DimMember[])al.toArray(new DimMember[0]));
				if (vrs != null) {
					for (int i=0; i<vrs.length; i++)
						al2.add(vrs[i]);
				}
			}
			catch (BusinessException be) {
				NtbLogger.error(be);
			}
		}
		super.setEnableTreeNodes((IBusiObj[])al2.toArray(new IBusiObj[0]), isEnable);
	}

}
